package com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager$ResolveTriggerType;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager.DnsFail;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsUtil;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.DNResolver.DNResolverCallback;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DNKeeper;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult.Address;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult.Address.Builder;
import java.util.ArrayList;
import java.util.List;

public class DNKeeperResolver extends DNResolver {
    private static final String TAG = "DNKeeperResolver";

    DNKeeperResolver(String str, DNResolverCallback dNResolverCallback) {
        super(str, 5, DNManager$ResolveTriggerType.DNS_SYNC_QUERY, dNResolverCallback);
    }

    DomainResult query() {
        int i = 0;
        Logger.v(TAG, "Resolve to DNKeeper, host: %s", this.domain);
        DomainResult domainResult = new DomainResult();
        DNKeeper dNKeeper = DNManager.getInstance().getDNKeeper();
        if (dNKeeper != null) {
            DnsFail dns = DNManager.getInstance().getDns(this.domain);
            String str = null;
            if (dns != null) {
                str = dns.getIp();
                i = dns.getCode();
            }
            DomainResult queryIpsSync = dNKeeper.queryIpsSync(this.domain, str, i);
            DNManager.getInstance().deleteDns(this.domain);
            domainResult = queryIpsSync;
        }
        if (!DnsUtil.isIpListEmpty(domainResult)) {
            Logger.v(TAG, "Resolve to DNKeeper, result: " + domainResult);
            List<Address> addressList = domainResult.getAddressList();
            List arrayList = new ArrayList();
            if (!addressList.isEmpty()) {
                for (Address address : addressList) {
                    arrayList.add(new Builder().type(address.getType()).value(address.getValue()).build());
                }
                domainResult.setAddressList(arrayList);
            }
            Logger.v(TAG, "Resolve to DNKeeper, result: " + domainResult);
        }
        return domainResult;
    }
}
